package database;

import java.util.*;

import javax.swing.JFrame;

import server.gui.Server_GUI;
import sharedspace.*;

/**
 * Responsible for data transition from SQL server to the server
 * @author G3
 *
 */
public class TeacherControl {
	
	
	private Teacher teacher = null;
	private ApprovingGrade approvingGrade = null;
	private List<Teacher> list = null;
	private List<ApprovingGrade> aList = null;
	
	public TeacherControl(){
		
	}
	
	/**
	 * Generate list of all teachers in database
	 * @return ArrayList<Teacher> List
	 */
	public List<Teacher> getTeacherList(){
		int teacherSize = JDBC.countStatment("SELECT count(*) FROM users u WHERE u.userPermission=2;");
		list = new ArrayList<Teacher>();
		Object[][] result = new Object[teacherSize][User.NUM_OF_COLUMNS];
		JDBC.selectStatmentMatrix("SELECT * FROM users u WHERE u.userPermission=2;", result, Teacher.NUM_OF_COLUMNS, teacherSize);
		for(int i = 0; i<teacherSize; i++)
		{
			teacher = new Teacher();
			setTeacher(result, i);
			list.add(teacher);
		}
		return list;
	}
	
	/**
	 * Generate teachers list that are in specific course in specific year and semester
	 * @param courseID
	 * @param year
	 * @param semester
	 * @return ArrayList<Teacher> list
	 */
	public List<Teacher> getTeacherListByCourseID(String courseID,int year, int semester)
	{
		int teacherSize = JDBC.countStatment("SELECT count(*) " +
											 "FROM teacherincourse tic " +
											 "WHERE tic.courseID="+courseID+" AND tic.year="+ year+" AND tic.semester="+semester+";");
		list = new ArrayList<Teacher>();
		Object[][] result = new Object[teacherSize][1];
		JDBC.selectStatmentMatrix("SELECT tic.teacherID" +
								  "FROM teacherincourse tic" +
								  "WHERE tic.courseID="+courseID+" AND tic.year="+ year+" AND tic.semester="+semester+";", result, 1,teacherSize);
		for(int i = 0;i<teacherSize;i++)
		{
			teacher = new Teacher();
			teacher = getTeacherByTeacherID(result[i][1].toString());
			list.add(teacher);
		}
		
		return list;
				
	}
	
	/**
	 * Generate list of teachers by subject
	 * @param subjectID
	 * @return ArrayList<Teacher> list
	 */
	public List<Teacher> getTeacherListBySubjectID(String subjectID)
	{
		int teacherSize = JDBC.countStatment("SELECT count(*)" +
											 "FROM teacherinsubject tis " +
											 "WHERE tis.subjectID="+ subjectID +";");
		list = new ArrayList<Teacher>();
		Object[][] result = new Object[teacherSize][1];
		JDBC.selectStatmentMatrix("SELECT tis.userID " +
								  "FROM teacherinsubject tis " +
								  "WHERE tis.subjectID="+ subjectID +";", result, 1, teacherSize);
		for(int i = 0; i<teacherSize;i++)
		{
			teacher = new Teacher();
			teacher = getTeacherByTeacherID(result[i][1].toString());
			list.add(teacher);
		}
		
		
		return list;
	}
	
	/**
	 * Generate list of all student exams that teacher need to approve
	 * @param treacherID
	 * @return ArrayList<ApprovingGrade> list
	 */
	public List<ApprovingGrade> getApprovedGradeByTeacherID(String treacherID)
	{
		
		aList = new ArrayList<ApprovingGrade>();
		int approveSize = JDBC.countStatment("SELECT count(*) FROM aprovinggrade ag " +
											 "WHERE ag.teacherID='"+treacherID+"' AND ag.teacherApproveComplete=false;");
		Object[][] result = new Object[approveSize][ApprovingGrade.NUM_OF_COLOUMS];
		JDBC.selectStatmentMatrix("SELECT * FROM aprovinggrade ag " +
								  "WHERE ag.teacherID='"+treacherID+"' AND ag.teacherApproveComplete=false;"
											 , result, ApprovingGrade.NUM_OF_COLOUMS, approveSize);
		for (int i = 0; i<approveSize; i++)
		{
			setApprovingGrade(result, i);
			aList.add(approvingGrade);
			
		}
		
		
		return aList;
	}
	
	/**
	 * Add teacher request of extra time to database
	 * @param time
	 * @param reason
	 * @param tID
	 * @param user
	 */
	public void addTeacherRequest(int time, String reason, String tID, User user)
	{
		JDBC.insertStatment("INSERT into teacherrequest "+"value('"+tID+"','"+"Teacher " + user.getUserFirstName() + " " + user.getUserLastName() + " request: " +reason+"',0,"+time+",0);");
	}
	
	/**
	 * Check if the teacher already filled extension request
	 * @param executeTestID
	 * @return boolean
	 */
	public boolean checkExecuteTestIDInTeacherRequest(String ID)
	{
		return JDBC.existStatment(ID, "executeTestID", "teacherrequest");
	}
	
	/**
	 * Return teacher info by ID
	 * @param teacherID
	 * @return Teacher teacher
	 */
	public Teacher getTeacherByTeacherID(String teacherID)
	{
		teacher = new Teacher();
		Object[] result = new Object[Teacher.NUM_OF_COLUMNS];
		JDBC.selectStatment("SELECT * FROM users u WHERE u.userID="+ teacherID +";", result, Teacher.NUM_OF_COLUMNS);
		setTeacher(result);
		return teacher;
	}
	
	/**
	 * Initialize Teacher object info from database
	 * @param result
	 * @param row
	 */
	public void setTeacher(Object[][] result, int row)
	{
		teacher = new Teacher();
		teacher.setUserID(result[row][0].toString());
		teacher.setUserLastName(result[row][1].toString());
		teacher.setUserFirstName(result[row][2].toString());
		teacher.setUserPassword(result[row][3].toString());
		teacher.setUserPermission(Integer.parseInt(result[row][4].toString()));
		teacher.setUserLogged(Boolean.parseBoolean(result[row][5].toString()));
		teacher.setUserStatus(result[row][6].toString());
	}
	
	/**
	 * Initialize Teacher object info from database
	 * @param result
	 */
	public void setTeacher(Object[] result)
	{
		teacher = new Teacher();
		teacher.setUserID(result[0].toString());
		teacher.setUserLastName(result[1].toString());
		teacher.setUserFirstName(result[2].toString());
		teacher.setUserPassword(result[3].toString());
		teacher.setUserPermission(Integer.parseInt(result[4].toString()));
		teacher.setUserLogged(Boolean.parseBoolean(result[5].toString()));
		teacher.setUserStatus(result[6].toString());
	}
	
	/**
	 * Initialize ApprovingGrade object info from database
	 * @param result
	 * @param row
	 */
	public void setApprovingGrade(Object[][] result, int row)
	{
		approvingGrade = new ApprovingGrade();
		approvingGrade.setTeacher((new TeacherControl()).getTeacherByTeacherID(result[row][0].toString()));
		approvingGrade.setExecuteTest((new ExecuteTestControl()).getExecuteTestByExecuteTestID(result[row][1].toString()));
		//approvingGrade.setStudent((new StudentControl()).getStudentByStudentID(result[row][2].toString()));
		StudentInTest s = (new StudentControl()).getStudentInTestByExecuteTestID(approvingGrade.getExecuteTest().getExecuteTestID(), result[row][2].toString());
		approvingGrade.setStudentInTest(s);
		approvingGrade.setStudentFianlGrade(Integer.parseInt(result[row][6].toString()));
		
	}
	


}
